package com.lehuo.jyn;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lehuo.util.BbsUtil;
import com.lehuo.util.CartUtil;
import com.lehuo.util.ConnUtil;
import com.lehuo.util.RepairUtil;
import com.lehuo.util.StringUtil;

public class LehuoCartServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("doGet");
		doPost(req, resp);
	}

	@SuppressWarnings("unused")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		if (req == null) {
			return;
		}
		String action = req.getParameter("action");
		System.out.println("doPost action:" + action);
		switch (action) {
		case "addCart":
			addCart(req, resp);
			break;
		case "deleteCart":
			deleteCart(req, resp);
			break;
		case "queryCartByUserId":
			queryCartByUserId(req, resp);
			break;
		case "pay":
			pay(req, resp);
			break;
		default:
			break;
		}
	}

	public void addCart(HttpServletRequest req, HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		String product_id = req.getParameter("product_id");
		String product_name = req.getParameter("product_name");
		String product_images = req.getParameter("product_images");
		String product_price = req.getParameter("product_price");

		String num = req.getParameter("num");
		String addressee = req.getParameter("addressee");
		String address = req.getParameter("address");
		String tel = req.getParameter("tel");

		String courier_num = req.getParameter("courier_num");
		String state = req.getParameter("state");

		user_id = StringUtil.toutf(user_id);
		product_id = StringUtil.toutf(product_id);
		product_name = StringUtil.toutf(product_name);
		product_images = StringUtil.toutf(product_images);
		product_price = StringUtil.toutf(product_price);
		num = StringUtil.toutf(num);
		addressee = StringUtil.toutf(addressee);
		address = StringUtil.toutf(address);
		tel = StringUtil.toutf(tel);
		courier_num = StringUtil.toutf(courier_num);
		state = StringUtil.toutf(state);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = CartUtil.addCart(user_id, product_id, product_name,
					product_images, product_price, num, addressee, address,
					tel, courier_num, state, connection);
			System.out.println("addCart result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public void queryCartByUserId(HttpServletRequest req,
			HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		user_id = StringUtil.toutf(user_id);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = CartUtil.queryCartByUserId(user_id, connection);
			System.out.println("queryCartByUserId result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteCart(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = CartUtil.deleteCart(id, connection);
			System.out.println("deleteCart result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	

	public void pay(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = CartUtil.pay(id, connection);
			System.out.println("queren result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
